/*
 * global_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <string.h>
#include "../head/gui_callback/global_cb.h"
#include "../head/communicate_with_server/userinfo_transfer.h"
#include "../head/gui_callback/user_login_cb.h"

extern GtkBuilder *builder;
extern int socketfd_login;
static int CURRENT_WINDOW = LOGIN; //当前显示的窗口
static bool login_flag = false;

//切换当前显示的窗口
void window_show(int windowshown) {
	hide_current_window();
	GtkWidget *show_window;
	switch (windowshown) {
	case GLOBAL_WELCOME_BODY:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_global_welcome_body"));
		break;
	case LOGIN:
		show_window = GTK_WIDGET(gtk_builder_get_object(builder, "vbox_login"));
		break;
	case MY_INFO_PERSONAL_INFO:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_personal_info"));
		break;
	case MY_INFO_CHANGE_PASSWORD:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_change_password"));
		break;
	case MY_INFO_EDIT_PERSONAL_INFO:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_edit_personal_info"));
		break;
	case MY_ORDER_SEARCH_ORDER:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_order"));
		break;
	case MY_ORDER_REFUND_TICKET:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_refund_ticket"));
		break;
	case BUY_TICKET:
		show_window = GTK_WIDGET(gtk_builder_get_object(builder, "vbox_buy"));
		break;
	case SEARCH_TICKET_LEFT:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search"));
		break;
	case QUERY_SALETIME:
		show_window = GTK_WIDGET(gtk_builder_get_object(builder, "vbox_query"));
		break;
	case SEARCH_TRAIN_INFO_TRAIN_NUMBER:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_train_number"));
		break;
	case SEARCH_TRAIN_INFO_START_END_STATION:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_start_end_station"));
		break;
	case SEARCH_TRAIN_INFO_STATION_PASS_TRAIN:
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_station_pass_train"));
		break;
	case REGISTER: {
		show_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_register"));
		break;
	}
	default:
		printf("global_cb.c: window_show error!\n");
		exit(EXIT_FAILURE);
		break;
	}
	gtk_widget_show(show_window);
	CURRENT_WINDOW = windowshown;
}

//隐藏当前显示的窗口
void hide_current_window() {
	GtkWidget *current_window;
	switch (CURRENT_WINDOW) {
	case GLOBAL_WELCOME_BODY:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_global_welcome_body"));
		break;
	case LOGIN:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_login"));
		break;
	case MY_INFO_PERSONAL_INFO:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_personal_info"));
		break;
	case MY_INFO_CHANGE_PASSWORD:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_change_password"));
		break;
	case MY_INFO_EDIT_PERSONAL_INFO:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_edit_personal_info"));
		break;
	case MY_ORDER_SEARCH_ORDER:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_order"));
		break;
	case MY_ORDER_REFUND_TICKET:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_refund_ticket"));
		break;
	case BUY_TICKET:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_buy"));
		break;
	case SEARCH_TICKET_LEFT:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search"));
		break;
	case QUERY_SALETIME:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_query"));
		break;
	case SEARCH_TRAIN_INFO_TRAIN_NUMBER:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_train_number"));
		break;
	case SEARCH_TRAIN_INFO_START_END_STATION:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_start_end_station"));
		break;
	case SEARCH_TRAIN_INFO_STATION_PASS_TRAIN:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_search_station_pass_train"));
		break;
	case REGISTER:
		current_window =
				GTK_WIDGET(gtk_builder_get_object(builder, "vbox_register"));
		break;
	default:
		perror("global_cb.c: hide_current_window error!\n");
		exit(EXIT_FAILURE);
		break;
	}
	gtk_widget_hide(current_window);
}

//处理工具栏中的用户退出的点击事件
void toolbutton_quit_clicked_cb(GtkToolButton *toolbutton, gpointer user_data) {
	bool response;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	response = gtk_show_question(global_window, "%s", "确认要退出吗？", "退出提醒");

	if (response) { //用户确定退出
		set_login_flag(false);
		struct userinfo *user = get_user_info();
		char *user_type = user->type;
		if (is_manager(user_type)) { //用户是管理员

		} else { //用户不是管理员
			GtkToolbar * toolbar =
					GTK_TOOLBAR(gtk_builder_get_object(builder,"toolbar"));
			gtk_widget_hide(GTK_WIDGET(toolbar) );
			window_show(LOGIN);
		}
	}
}

//处理窗口关闭事件
void on_window_destroy(GtkWidget *window, gpointer user_data) {
	gtk_main_quit();
	g_object_unref(G_OBJECT(builder) );
	close(socketfd_login);
}

//检查用户是否已经登录
bool check_is_login() {
	return login_flag;
}

//设置标记表明用户是否登录
void set_login_flag(bool islogin) {
	login_flag = islogin;
}

//显示信息窗口
void gtk_show_info(GtkWindow *window, const gchar* message_format,
		const gchar* message, const gchar* title) {
	GtkWidget *dialog;
	dialog = gtk_message_dialog_new_with_markup(window,
			GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
			message_format, message);
	gtk_window_set_title(GTK_WINDOW(dialog), title);
	gtk_dialog_run(GTK_DIALOG(dialog) );
	gtk_widget_destroy(GTK_WIDGET(dialog) );
}

//显示错误提示窗口
void gtk_show_error(GtkWindow *window, const gchar* message_format,
		const gchar* message, const gchar* title) {
	GtkWidget *dialog;
	dialog = gtk_message_dialog_new_with_markup(window,
			GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
			message_format, message);
	gtk_window_set_title(GTK_WINDOW(dialog), title);
	gtk_dialog_run(GTK_DIALOG(dialog) );
	gtk_widget_destroy(dialog);
}

//显示询问窗口
bool gtk_show_question(GtkWindow *window, const gchar* message_format,
		const gchar* message, const gchar* title) {
	gint response;
	GtkWidget *dialog;

	dialog = gtk_message_dialog_new_with_markup(window,
			GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
			GTK_BUTTONS_YES_NO, message_format, message);
	gtk_window_set_title(GTK_WINDOW(dialog), title);
	response = gtk_dialog_run(GTK_DIALOG(dialog) );
	gtk_widget_destroy(dialog);

	if (response == GTK_RESPONSE_YES) {
		return true;
	} else {
		return false;
	}
}

//显示警告窗口
void gtk_show_warning(GtkWindow *window, const gchar* message_format,
		const gchar* message, const gchar* title) {
	GtkWidget *dialog;
	dialog = gtk_message_dialog_new_with_markup(window,
			GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
			message_format, message);
	gtk_window_set_title(GTK_WINDOW(dialog), title);
	gtk_dialog_run(GTK_DIALOG(dialog) );
	gtk_widget_destroy(dialog);
}

//处理日期输入框获得焦点的事件
void entry_focus_in_event_cb(GtkWidget *widget, GdkEvent *event,
		gpointer user_data) {
	GtkCalendar *cal = GTK_CALENDAR(user_data);
	gtk_widget_show((GtkWidget*) cal);
	gtk_widget_grab_focus((GtkWidget*) cal);
}

//处理日期选择框的天的双击事件
void calendar_day_selected_double_click_cb(GtkCalendar *calendar,
		gpointer user_data) {
	gtk_widget_hide(GTK_WIDGET(calendar) );
	guint year;
	guint month;
	guint day;
	gtk_calendar_get_date(calendar, &year, &month, &day);
	char date[BIRTHDAY_LEN];
	sprintf(date, "%04d-%02d-%02d", year, ++month, day);
	GtkEntry *entry_birthday = GTK_ENTRY (user_data);
	gtk_entry_set_text(entry_birthday, date);
}

//处理日期选择框失去焦点的事件
void calendar_focus_out_event_cb(GtkWidget *widget, GdkEvent *event,
		gpointer user_data) {
	gtk_widget_hide(widget);
	guint year;
	guint month;
	guint day;
	gtk_calendar_get_date(GTK_CALENDAR(widget), &year, &month, &day);
	char date[BIRTHDAY_LEN];
	sprintf(date, "%04d-%02d-%02d", year, ++month, day);
	GtkEntry *entry_birthday = GTK_ENTRY(user_data);
	gtk_entry_set_text(entry_birthday, date);
}
